package com.yytek.job.task;

import com.alibaba.fastjson2.JSONObject;
import com.yytek.common.core.constant.CacheConstants;
import com.yytek.common.core.constant.ProtocolConstans;
import com.yytek.common.core.constant.ValueConstant;
import com.yytek.common.core.domain.*;
import com.yytek.common.core.utils.bean.BeanCopyUtils;
import com.yytek.common.redis.service.RedisService;
import com.yytek.job.domain.PlanTaskTemplate;
import com.yytek.job.domain.SysJob;
import com.yytek.job.fegin.MqttFegin;
import com.yytek.job.service.PlanTaskTemplateServiceImpl;
import com.yytek.job.service.SysJobServiceImpl;
import com.yytek.nmp.api.RemoteNmpService;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 网关协议发送任务
 */
@Component("gwTask")
public class GwTask
{
    @Resource
    private RedisService redisService;
    @Resource
    private RemoteNmpService remoteNmpService;
    @Resource
    private PlanTaskTemplateServiceImpl planTaskTemplateService;
    @Resource
    private SysJobServiceImpl sysJobService;
    @Resource
    private MqttFegin mqttFegin;
    @Resource
    private RestTemplate restTemplate;
    /**
     * 网关发送内容
     * @param taskTemplateId 任务模板id
     * @param iotId 设备id
     */
    public void sendMsg(String taskTemplateId,String iotId)
    {
        /**
         * 1.如果在线状态
         * 			 (1)MQTT调用 mqtt网关服务 写 接口
         * 			 (2)其他协议 检查存在连接的ip和端口调用 写 接口
         * 	2.如果没有连接信息,则不执行任务
         */
        if(remoteNmpService.getDeviceCache(iotId) != null
            && remoteNmpService.getDeviceCache(iotId).getData() != null ){
            DeviceCache deviceCache = BeanCopyUtils.mapToBean((Map<String, Object>)
                    remoteNmpService.getDeviceCache(iotId).getData(), DeviceCache.class);;
            if(deviceCache != null){

                //获取任务模板缓存
                PlanTaskTemplate planTaskTemplate = planTaskTemplateService
                        .selectPlanTaskTemplateCacheById(Long.valueOf(taskTemplateId));
                //发送内容缓存
                SysJob sysJob =sysJobService.getSysJobCache(taskTemplateId,iotId);
                //发送协议
                String protocol = planTaskTemplate.getProtocolType().toLowerCase();
                //获取发送信息
                CommandRequest commandRequest = JSONObject.parseObject(sysJob.getSendContent(),CommandRequest.class);
                //如果发送是子设备类型则用网关设备id
                if(deviceCache.getNodeType() == 2){
                    commandRequest.setDeviceId(deviceCache.getGateWayId());
                }
                //MQTT调用 mqtt网关服务 写 接口
                if(protocol.equals(ProtocolConstans.MQTT.toLowerCase())){
                    mqttFegin.write(commandRequest);
                }else{
                    //其他协议 检查存在连接的ip和端口调用 写 接口
                    //获取设备连接的信息
                    String key = CacheConstants.GW_DEVICE_CONECT_KEY+iotId;
                    DeviceConectInfo conectInfo =redisService.getCacheObject(key);
                    String targetIp = conectInfo.getIp();
                    String url =targetIp + ValueConstant.Driver.COMMAND_URL_PREFIX+"/write";
                    restTemplate.postForEntity(url,commandRequest,R.class);
                }

            }
        }

    }

}
